constructive algorithms implementation trees *1900

Please click on ads to support us..

Python Code:

t = int(input())
for _ in range(t):
    arr = input().strip().split()
    n, x, y, z = [int(i) for i in arr]
    v1, v2, v3 = 1, 2, 3
    if x > y:
        v1, v3 = v3, v1
        x, y = y, x
    if y > z:
        v1, v2 = v2, v1
        y, z = z, y
    if x > y:
        v1, v3 = v3, v1
        x, y = y, x
    if x + y < z:
        print("NO")
    elif (x + y - z) % 2 != 0:
        print("NO")
    else:
                                        
        lists = []
        t = (y + z - x) // 2
        k = (x + y - z) // 2

        if  x + t + 1 > n:
            print("NO")
        else:
            print("YES")
            last_used_point = 3

            if k == 0:
                pred_point = v2
                for i in range(x):
                    if i != x - 1:
                        last_used_point += 1
                        lists.append([pred_point, last_used_point])
                        pred_point = last_used_point
                    else:
                        lists.append([pred_point, v1])
                pred_point = v2
                for i in range(y):
                    if i != y - 1:
                        last_used_point += 1
                        lists.append([pred_point, last_used_point])
                        pred_point = last_used_point
                    else:
                        lists.append([pred_point, v3])
            else:
                traffic_point = 4
                last_used_point = 4

                pred_point = v2
                for i in range(k):
                    if i != k - 1:
                        last_used_point += 1
                        lists.append([pred_point, last_used_point])
                        pred_point = last_used_point
                    else:
                        lists.append([pred_point, traffic_point])

                pred_point = traffic_point
                for i in range(x - k):
                    if i != x - k - 1:
                        last_used_point += 1
                        lists.append([pred_point, last_used_point])
                        pred_point = last_used_point
                    else:
                        lists.append([pred_point, v1])

                pred_point = traffic_point
                for i in range(t):
                    if i != t - 1:
                        last_used_point += 1
                        lists.append([pred_point, last_used_point])
                        pred_point = last_used_point
                    else:
                        lists.append([pred_point, v3])

            for i in range(last_used_point+1, n+1):
                lists.append([1, i])

            for edge in lists:
                print(edge[0], edge[1])


C++ Code:

#include<bits/stdc++.h>
#define pb push_back
using namespace std;
int T,n,now,d[5],a[5];
void print(int u,int v,int dis){
	if(u==v)return;
	vector<int> V;V.pb(u);
	--dis;
	while(dis--)V.pb(++now);
	V.pb(v);
	for(int i=0;i<V.size()-1;i++)printf("%d %d\n",V[i],V[i+1]);
	return;
}
int main(){
	scanf("%d",&T);
	while(T--){
		scanf("%d%d%d%d",&n,&d[1],&d[2],&d[3]);
		int s=d[1]+d[2]+d[3];
		if(s&1){puts("NO");continue;}
		s>>=1;
		a[1]=s-d[2],a[2]=s-d[3],a[3]=s-d[1];
		if(a[1]<0||a[2]<0||a[3]<0||a[1]+a[2]+a[3]+1>n){puts("NO");continue;}
		puts("YES");
		now=3;int rt=0;
		for(int i=1;i<=3;i++)
			if(!a[i]){rt=i;break;}
		if(!rt)rt=++now;
		for(int i=1;i<=3;i++)print(rt,i,a[i]);
		for(++now;now<=n;now++)printf("1 %d\n",now);
	}
	return 0;
}


Comments

Submit
0 Comments
More Questions

520A - Pangram
124A - The number of positions
1041A - Heist
901A - Hashing Trees
1283A - Minutes Before the New Year
1654D - Potion Brewing Class
1107B - Digital root
25A - IQ test
785A - Anton and Polyhedrons
1542B - Plus and Multiply
306A - Candies
1651C - Fault-tolerant Network
870A - Search for Pretty Integers
1174A - Ehab Fails to Be Thanos
1169A - Circle Metro
780C - Andryusha and Colored Balloons
1153A - Serval and Bus
1487C - Minimum Ties
1136A - Nastya Is Reading a Book
1353B - Two Arrays And Swaps
1490E - Accidental Victory
1335A - Candies and Two Sisters
96B - Lucky Numbers (easy)
1151B - Dima and a Bad XOR
1435B - A New Technique
1633A - Div 7
268A - Games
1062B - Math
1294C - Product of Three Numbers
749A - Bachgold Problem